<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div style="height: 1000px;" class="box">onscroll</div>
    <button style="position: fixed;right: 100px;bottom: 100px;display: none;">↑</button>
    <script>
    
        let btn=document.querySelector("button")
        function fun(fn){
            let time=null;
            function eve(){
                if(time!=null){
                    clearTimeout(time)
                }
                time=setTimeout(() => {
                    fn()
                    time=null
                }, 500);
            }
            return eve
        }
        onscroll=fun(()=>{
            if(document.documentElement.scrollTop>0){
                btn.style.display="block";
                console.log(1)
            }else{
                btn.style.display="none"
                console.log(2)
            }
        })
        btn.addEventListener("click",function(){
            document.documentElement.scrollTop=0
        })
    </script>
</body>
</html>